package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecGroupService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> querySpecGroupByCid(Long cid){
        SpecGroup group = new SpecGroup();
        group.setCid(cid);
        List list = this.specGroupMapper.select(group);
        return list;
    }

    public List<SpecParam> querySpecParamBuGid(Long gid,Long cid,Boolean searching,Boolean generic){
        SpecParam param = new SpecParam();
        param.setGroupId(gid);
        param.setCid(cid);
        param.setSearching(searching);
        param.setGeneric(generic);
        List<SpecParam> params = this.specParamMapper.select(param);
        return params;
    }

    public void addSpecGroup(SpecGroup group){
        this.specGroupMapper.insert(group);
    }

    public void updateSpencGroup (SpecGroup group){
        this.specGroupMapper.updateByPrimaryKeySelective(group);
    }

    public void deleteSpecGroupById(Long id){
        this.specGroupMapper.deleteByPrimaryKey(id);
    }

    public void addSpecParam(SpecParam param){
        this.specParamMapper.insert(param);
    }

    public void updateSpecParam(SpecParam param){
        this.specParamMapper.updateByPrimaryKeySelective(param);
    }

     public void deleteSpecParamById(Long id){
        this.specParamMapper.deleteByPrimaryKey(id);
     }

    public List<SpecGroup> querySpecByCid(Long cid){
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> specGroups = this.specGroupMapper.select(specGroup);
        specGroups.forEach(s -> {
            s.setParams(this.querySpecParamBuGid(s.getId(),null,null,null));
        });
        return specGroups;
    }

}
